// pages/user/user.js
import { request } from "../../request/index.js"
import { navigateTo } from "../../utils/asyncWx.js"
import { isNotEmptyStr } from "../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      user: {},
      joinNumber: NaN,
      checkedNumber: NaN,
      managerNumber: NaN,
      attendanceRate: NaN
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoadUser(options) {
    console.log("onLoadUser is triggered!")
    // 发送
    // await this.getUserInfo()
  },
  async onShowUser() {
    console.log("onShowUser is triggered!")

    // 先从缓存中取昵称和头像
    // let avatarUrl = wx.getStorageSync('avatarUrl')
    // let nickName = wx.getStorageSync('nickName')
    // 没有再请求
    // if(isNotEmptyStr(avatarUrl) && isNotEmptyStr(nickName)){
    //   let userInfo = { user: { nickName, avatarUrl } }
    //   this.setData({ userInfo })
    // }else{
      await this.getUserInfo()
    // }
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    // console.log(avatarUrl)
    let userInfo = { avatarUrl }
    this.setData({ userInfo })

  },
  // 请求用户信息
  async getUserInfo() {
    const res = await request({ url: "/WxUser/getUserInfo"})
    let userInfo = res.data.data
    this.setData({ userInfo })
    // console.log(res)

    // 存入缓存
    wx.setStorageSync('avatarUrl', userInfo.user.avatarUrl)
    wx.setStorageSync('nickName', userInfo.user.nickName)

  },
  // 跳转到录入信息界面
  handleJump(){
    navigateTo({url: "/pages/fillInfo/fillInfo"})
  }
})